package ebusiness.ebusiness.Dao.WalletDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Entity.Order;
import ebusiness.ebusiness.Entity.wallet;
import ebusiness.ebusiness.Entity.walletflow;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class WalletQueryVO {
    private static final String waitforGood="待收货";
    public int addWallet(int uid)throws Exception{

        return 0;
    }

    public wallet getWalletByID(int id) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            WalletQuery walletQuery=sqlSession.getMapper(WalletQuery.class);
            wallet wallet=walletQuery.getWalletByID(id);
            sqlSession.close();
            return wallet;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }
    public int PayOrder(int walletID,int password,int paytype,List<Double> price,List<Integer> orderID) throws Exception{
        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        try {
            WalletQuery walletQuery=sqlSession.getMapper(WalletQuery.class);
            wallet wa = walletQuery.getWalletByID(walletID);

            if(wa!=null&&password==wa.getWallet_pw()){
                double remain=wa.getWallet_remain();
                double total=0;

                for(double d:price)total+=d;
                int l=orderID.size();
                if(l<=0){
                    sqlSession.close();
                    return -5;
                }
                double before;

                if(remain>total){
                    for(int i=0;i<l;i++){
                        System.out.print(orderID.get(i));
                        before=remain;
                        remain-=price.get(i);
                        Date date=new Date();
                        java.sql.Date dt=new java.sql.Date(date.getTime());

                        walletQuery.PayOrder(walletID,password,remain,dt.toString());
                        walletQuery.Wateflow(orderID.get(i),orderID.get(i),wa.getCustomer_id(),before,paytype,price.get(i),remain,dt.toString());
                        walletQuery.PayOrder2(orderID.get(i),waitforGood,dt.toString());
                    }

                    sqlSession.commit();
                    sqlSession.close();
                    return 1;
                }{
                    sqlSession.close();
                    return -10;
                }

            }
        }
        catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            sqlSession.close();
            return -1;
        }

            sqlSession.close();

        return -2;
    }

    public List<walletflow> getwalletflowByUId(int uid) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            WalletQuery walletQuery=sqlSession.getMapper(WalletQuery.class);
            List<walletflow> walletList=walletQuery.getwalletflowByUId(uid);
            sqlSession.close();
            return walletList;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.close();
        }return null;

    }

    public List<wallet> getUserwalletByUId(int uid) throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            WalletQuery walletQuery=sqlSession.getMapper(WalletQuery.class);
            List<wallet> walletList=walletQuery.getUserwalletByUId(uid);
            sqlSession.close();
            return walletList;
        }catch (Exception e
                ){
            e.printStackTrace();
            sqlSession.close();
            return null;
        }

    }

}
